package com.spera.shopoa.util;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.spring.common.web.service.CommonService;
import com.spring.security.core.context.JedisCont;
import com.spring.security.core.util.ApplicationUtils;
import com.spring.security.core.util.CookieUtils;
import com.spring.security.web.iputil.HttpClientUtils;
import com.spring.tools.SpringContextHolder;

/**
 * OA权限
 * @author wojunyu
 *
 */
public class ShopOaSessionUtil {
	
	private static String OACOOKIENAME = "PHPSESSID";
	public static String SHOPOA_USER = "shopoauser";
	public static String SHOPOA_ROLE = "shopoarole";
	public static String SHOPOA_MENU = "shopoamenu";
	
	private static CommonService commonService = SpringContextHolder.getBean(CommonService.class);
	
	public static String APPPATH = "/SperaSer/ser";
	
	private static String permission="{'status':'1','msg':'','dataset':[{'LOGIN_USER_ID':'WV00000069',"+
				"'LOGIN_USER_ACCOUNTS':'内部系统管理员','LOGIN_USER_NAME':'内部系统管理员',"+
				"'LOGIN_USER_PRIV':'32','LOGIN_DEPT_ID':'28','LOGIN_DEPT_NAME':'广州旗舰店',"+
				"'FILIALE_DEPT_ID':'27','FILIALE_DEPT_NAME':'广州蒂芭卜','SHOP_DEPT_ID':'28',"+
				"'SHOP_DEPT_NAME':'广州旗舰店'}]}";
	private static String userInfo="{"+
			"  'user_info': ["+
			"    {"+
			"      'USER_SUB_PRIV': ',',"+
			"      'LOGIN_LANG_ID': '1',"+
			"      'LOGIN_LANG': 'cn',"+
			"      'LOGIN_USER_ID': 'WV00000069',"+
			"      'LOGIN_PASSWORD': '$1$n73.kI3.$hYhkWC0bgcfP/C9p2qmnM/',"+
			"      'LOGIN_POST_PRIV': '0',"+
			"      'LOGIN_USER_ACCOUNTS': '内部系统管理员',"+
			"      'LOGIN_USER_NAME': '内部系统管理员',"+
			"      'LOGIN_USER_PRIV': '32',"+
			"      'LOGIN_DEPT_ID': '28',"+
			"      'LOGIN_AVATAR': '1',"+
			"      'LOGIN_THEME': 'theme21',"+
			"      'LOGIN_FUNC_STR': '1,2,3,252,323,420,5,377,6,8,7,314,237,130,131,11,13,12,14,32,17,26,181,27,30,362,363,364,215,20,21,22,245,216,23,25,249,250,700,701,702,160,161,162,165,167,168,169,170,171,172,530,531,532,207,208,210',"+
			"      'SMS_LOGIN': '0',"+
			"      'LOGIN_USBKEY': '0',"+
			"      'USBKEY_PIN': ''"+
			"    }"+
			"  ]"+
			"}";
	/**
	 * user_info:{"LOGIN_DEPT_ID":"13",
	"LOGIN_LANG":"cn",
	"LOGIN_LANG_ID":"1",
	"USER_SUB_PRIV":false,
	"SMS_LOGIN":"0",
	"LOGIN_USER_ID":"WV00000018",
	"LOGIN_USER_PRIV":"5",
	"USBKEY_PIN":"",
	"LOGIN_USER_NAME":"沃俊宇",
	"LOGIN_THEME":"theme11",
	"LOGIN_USBKEY":"0",
	"LOGIN_AVATAR":"",
	"LOGIN_PASSWORD":"$1$rj1.Yd5.$YJGy5bUOoKEHDGa0./pkq0",
	"LOGIN_POST_PRIV":"0",
	"LOGIN_USER_ACCOUNTS":"沃俊宇"}
	user_role:[]
	user_priv:[]
	 * @param request
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static boolean checkOaSession(HttpServletRequest request){
		String cookie = CookieUtils.getCookie(request, OACOOKIENAME);
		System.out.println("cookie---------->"+cookie);
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("phpsessid", cookie);
		String json=null;
		
		String isDevelopment = JedisCont.getProParm("isDevelopment");
		if(isDevelopment!=null&&isDevelopment.equals("1")){
			json=permission;
		}else{
			json = HttpClientUtils.httpPost(JedisCont.getProParm("ipad.user_url")+"/WWWphpsessid.php",param);
		}
		System.out.println(json);

		Map<String, Object> map = JSON.parseObject(json, new TypeReference<Map<String, Object>>(){});
		
		List<Map<String,Object>> userList = (List<Map<String,Object>>)map.get("user_info");
		List<Map<String,Object>> userPriv = (List<Map<String,Object>>)map.get("user_priv");
		if(userList!=null&&userList.size()>0){
			request.getSession().removeAttribute(SHOPOA_USER);
			request.getSession().removeAttribute(SHOPOA_ROLE);
			request.getSession().removeAttribute(SHOPOA_MENU);
			request.getSession().setAttribute(SHOPOA_USER, userList.get(0));
			request.getSession().setAttribute(SHOPOA_ROLE, map.get("user_role"));
			request.getSession().setAttribute(SHOPOA_MENU, map.get("user_priv"));
			
			String url = request.getRequestURL().toString();
			boolean hasPriv = false;
			CharSequence oaPriv = "";
			for(Map<String,Object> m:userPriv){
				oaPriv = (String)m.get("func_url");
				if(ApplicationUtils.isNotNull(oaPriv)&&url.contains(oaPriv)){
					hasPriv = true;
				}
			}
			return hasPriv;
		}else{
			return false;
		}
	}
	
	/**
	 * 验证用户及菜单权限
	 * @param request
	 * @return
	 * {
		  "status": "1",
		  "msg": "",
		  "dataset": [
		    {
		      "LOGIN_USER_ID": "WV00000033",
		      "LOGIN_USER_ACCOUNTS": "门店员工",
		      "LOGIN_USER_NAME": "门店员工",
		      "LOGIN_DEPT_ID": "30",
		      "LOGIN_DEPT_NAME": "销售部",
		      "FILIALE_DEPT_ID": "27",
		      "FILIALE_DEPT_NAME": "广州分公司",
		      "SHOP_DEPT_ID": "28",
		      "SHOP_DEPT_NAME": "旗舰店"
		    }
		  ]
		}
	 */
	@SuppressWarnings("unchecked")
	public static boolean checkOaPriv(HttpServletRequest request,String url){
		String cookie = CookieUtils.getCookie(request, OACOOKIENAME);
		Map<String, Object> param = new HashMap<String, Object>();
		
		param.put("phpsessid", cookie);				
		param.put("url", url);

		String json = null;		
		
		String isDevelopment = JedisCont.getProParm("isDevelopment");
		if(isDevelopment!=null&&isDevelopment.equals("1")){
			json=permission;
		}else{
			json = HttpClientUtils.httpPost(JedisCont.getProParm("ipad.user_url")+"/WWWphpsessid_url.php",param);
		}		
		System.out.println(json);		
		
		Map<String, Object> map = JSON.parseObject(json, new TypeReference<Map<String, Object>>(){});
		String status = ApplicationUtils.Object2String(map.get("status"));
		if("0".equals(status)){
			return false;
		}else{
			List<Map<String,Object>> l = (List<Map<String,Object>>)map.get("dataset");
			if(l!=null&&l.size()>0){
				Map<String,Object> m = l.get(0);
				OaUser oaUser = new OaUser();
				oaUser.setOa_user_id(ApplicationUtils.Object2String(m.get("LOGIN_USER_ID")))
					.setOa_user_accounts(ApplicationUtils.Object2String(m.get("LOGIN_USER_ACCOUNTS")))
					.setOa_user_name(ApplicationUtils.Object2String(m.get("LOGIN_USER_NAME")))
					.setUser_dept_id(ApplicationUtils.Object2String(m.get("LOGIN_DEPT_ID")))
					.setUser_dept_name(ApplicationUtils.Object2String(m.get("LOGIN_DEPT_NAME")))
					.setUser_compdept_id(ApplicationUtils.Object2String(m.get("FILIALE_DEPT_ID")))
					.setUser_compdept_name(ApplicationUtils.Object2String(m.get("FILIALE_DEPT_NAME")))
					.setUser_shopdept_id(ApplicationUtils.Object2String(m.get("SHOP_DEPT_ID")))
					.setUser_shopdept_name(ApplicationUtils.Object2String(m.get("SHOP_DEPT_NAME")));
				if(ApplicationUtils.isNotNull(oaUser.getUser_shopdept_id())){
					String kids_park_id = commonService.getKidParkIDByDeptID(oaUser.getUser_shopdept_id());
					if(ApplicationUtils.isNotNull(kids_park_id)){
						oaUser.setKids_park_id(kids_park_id);
					}
				}
				setOaUser(request,oaUser);
			}
			return true;
		}
	}
	@SuppressWarnings("unchecked")
	public static boolean setOaSession(HttpServletRequest request){
		String cookie = CookieUtils.getCookie(request, OACOOKIENAME);
		
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("phpsessid", cookie);
		String json=null;
		
		String isDevelopment = JedisCont.getProParm("isDevelopment");
		if(isDevelopment!=null&&isDevelopment.equals("1")){
			json=userInfo;
		}else{
			json = HttpClientUtils.httpPost(JedisCont.getProParm("ipad.user_url")+"/WWWphpsessid.php",param);
		}
		System.out.println(json);

		Map<String, Object> map = JSON.parseObject(json, new TypeReference<Map<String, Object>>(){});
	
		List<Map<String,Object>> userList = (List<Map<String,Object>>)map.get("user_info");
		if(userList!=null&&userList.size()>0){
			Map<String,Object> m = userList.get(0);
			
			OaUser oaUser = new OaUser();
			oaUser.setOa_user_id(ApplicationUtils.Object2String(m.get("LOGIN_USER_ID")))
				.setOa_user_accounts(ApplicationUtils.Object2String(m.get("LOGIN_USER_ACCOUNTS")))
				.setOa_user_name(ApplicationUtils.Object2String(m.get("LOGIN_USER_NAME")))
				.setUser_dept_id(ApplicationUtils.Object2String(m.get("LOGIN_DEPT_ID")))
				.setUser_dept_name(ApplicationUtils.Object2String(m.get("LOGIN_DEPT_NAME")))
				.setUser_compdept_id(ApplicationUtils.Object2String(m.get("FILIALE_DEPT_ID")))
				.setUser_compdept_name(ApplicationUtils.Object2String(m.get("FILIALE_DEPT_NAME")))
				.setUser_shopdept_id(ApplicationUtils.Object2String(m.get("SHOP_DEPT_ID")))
				.setUser_shopdept_name(ApplicationUtils.Object2String(m.get("SHOP_DEPT_NAME")));
			
			setOaUser(request,oaUser);
		}		
		return true;
	}

	public static void setOaUser(HttpServletRequest request,OaUser oaUser){
		request.getSession().removeAttribute(SHOPOA_USER);
		request.getSession().setAttribute(SHOPOA_USER, oaUser);
	}
	
	public static OaUser getOaUser(HttpServletRequest request){
		return (OaUser)request.getSession().getAttribute(SHOPOA_USER);
	}
	
	
	/**
	 * 授权用户信息
	 */
	public static class OaUser implements Serializable {

		private static final long serialVersionUID = 1L;
		
		private String oa_user_id; // 编号
		private String oa_user_accounts; // 登录名
		private String oa_user_name; // 姓名
		private String user_dept_id;//部门编号
		private String user_dept_name;
		private String user_compdept_id;//分公司编号
		private String user_compdept_name;
		private String user_shopdept_id;//门店编号
		private String user_shopdept_name;
		private String kids_park_id;//对应自己系统数据库门店编号
		
		public String getOa_user_id() {
			return oa_user_id;
		}
		public OaUser setOa_user_id(String oa_user_id) {
			this.oa_user_id = oa_user_id;
			return this;
		}
		public String getOa_user_accounts() {
			return oa_user_accounts;
		}
		public OaUser setOa_user_accounts(String oa_user_accounts) {
			this.oa_user_accounts = oa_user_accounts;
			return this;
		}
		public String getOa_user_name() {
			return oa_user_name;
		}
		public OaUser setOa_user_name(String oa_user_name) {
			this.oa_user_name = oa_user_name;
			return this;
		}
		public String getUser_dept_id() {
			return user_dept_id;
		}
		public OaUser setUser_dept_id(String user_dept_id) {
			this.user_dept_id = user_dept_id;
			return this;
		}
		public String getUser_dept_name() {
			return user_dept_name;
		}
		public OaUser setUser_dept_name(String user_dept_name) {
			this.user_dept_name = user_dept_name;
			return this;
		}
		public String getUser_compdept_id() {
			return user_compdept_id;
		}
		public OaUser setUser_compdept_id(String user_compdept_id) {
			this.user_compdept_id = user_compdept_id;
			return this;
		}
		public String getUser_compdept_name() {
			return user_compdept_name;
		}
		public OaUser setUser_compdept_name(String user_compdept_name) {
			this.user_compdept_name = user_compdept_name;
			return this;
		}
		public String getUser_shopdept_id() {
			return user_shopdept_id;
		}
		public OaUser setUser_shopdept_id(String user_shopdept_id) {
			this.user_shopdept_id = user_shopdept_id;
			return this;
		}
		public String getUser_shopdept_name() {
			return user_shopdept_name;
		}
		public OaUser setUser_shopdept_name(String user_shopdept_name) {
			this.user_shopdept_name = user_shopdept_name;
			return this;
		}
		public String getKids_park_id() {
			return kids_park_id;
		}
		public OaUser setKids_park_id(String kids_park_id) {
			this.kids_park_id = kids_park_id;
			return this;
		}
	}
}
